//
//  HWGameCommunityListProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/18.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommunityListProtocol: NSObject {
    
    var models:[HWGameCommunityListModel]?
    
    var scrollDidScroll:((_:UIScrollView)->())?
    /// 点击图片
    var didSelectImage:((_:Int,_:[String])->())?
    /// 点击视频
    var didSelectVideo:((_:String)->())?
    
    var jumpToDetail:((_:HWGameCommunityListModel)->())?
    
    var zanButtonClick:((_:HWGameCommunityListModel)->())?
    
    var delButtonClick:((_:HWGameCommunityListModel)->())?
    
    var height = 0.0
}

extension HWGameCommunityListProtocol: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (models?.count ?? 0) > 0 {
            if (models?[indexPath.section].video.count ?? 0) > 0 {
                didSelectVideo?(models?[indexPath.section].video ?? "")
            } else {
                if (models?[indexPath.section].image.count ?? 0 ) > 0 {
                    didSelectImage?(indexPath.row,models?[indexPath.section].image ?? [])
                } else {
                    
                }
            }
        } else {
            
        }
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionView.elementKindSectionHeader {
            let  header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWGameCommunityListHeader", for: indexPath) as! HWGameCommunityListHeader
            header.model = models?[indexPath.section]
            header.jumpToDetail = {
                self.jumpToDetail?($0)
            }
            return header
            
        }else{
            let  footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWGameCommunityListFooter", for: indexPath) as! HWGameCommunityListFooter
            footer.model = models?[indexPath.section]
            footer.jumpToDetail = {
                self.jumpToDetail?($0)
            }
            footer.zanButtonClick = {
                self.jumpToDetail?($0)
            }
            footer.delButtonClick = {
                self.delButtonClick?($0)
            }
            return footer
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if getTextHeigh(textStr: models?[section].content ?? "", font: .systemFont(ofSize: 14*widthScale), width: 339*widthScale) > 40*widthScale {
            height += 150*widthScale
            return CGSize(width: screenWidth, height: 150*widthScale)
        } else {
            height += 150*widthScale
            return CGSize(width: screenWidth, height: getTextHeigh(textStr: models?[section].content ?? "", font: .systemFont(ofSize: 14*widthScale), width: 339*widthScale) + 110*widthScale)
        }
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        height += 67*widthScale
        return CGSize(width: screenWidth, height: 67*widthScale)
        
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        height += 6*widthScale
        return 6*widthScale
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        height += 6*widthScale
        return 6*widthScale
    }
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//        
//        if (models?.count ?? 0) > 0 {
//            if (models?[section].video.count ?? 0) > 0 {
//                return UIEdgeInsets(top: 0, left: 18*widthScale, bottom: 0, right: 18*widthScale)
//            } else {
//                if (models?[section].image.count ?? 0 ) > 0 {
//                    if (models?[section].image.count ?? 0 ) == 1 {
//                        return UIEdgeInsets(top: 0, left: 18*widthScale, bottom: 0, right: 149*widthScale)
//                    } else {
//                        return UIEdgeInsets(top: 0, left: 18*widthScale, bottom: 0, right: 18*widthScale)
//                    }
//                    
//                } else {
//                    return .zero
//                }
//            }
//        } else {
//            return .zero
//        }
//    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if (models?.count ?? 0) > 0 {
            if (models?[indexPath.section].video.count ?? 0) > 0 {
                height += 163*widthScale
                return CGSize(width: CGFloat.screenWidth - 24*widthScale, height: 163*widthScale)//CGSize(width: 339*widthScale, height: 163*widthScale)
            } else {
                if (models?[indexPath.section].image.count ?? 0 ) > 0 {
                    if (models?[indexPath.section].image.count ?? 0 ) == 1 {
                        height += 109*widthScale
                        return CGSize(width: CGFloat.screenWidth - 24*widthScale, height: 109*widthScale)//CGSize(width: 208*widthScale, height: 109*widthScale)
                    } else {
                        height += 109*widthScale
                        return CGSize(width: CGFloat.screenWidth - 24*widthScale, height: 109*widthScale)//CGSize(width: 109*widthScale, height: 109*widthScale)
                    }
                    
                } else {
                    return CGSize.zero
                }
            }
        } else {
            return CGSize.zero
        }
        
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return models?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (models?.count ?? 0) > 0 {
            if (models?[section].video.count ?? 0) > 0 {
                return 1
            } else {
                if (models?[section].image.count ?? 0 ) > 0 {
                    if (models?[section].image.count ?? 0 ) == 1 {
                        return 1
                    } else {
                        return models?[section].image.count ?? 0
                    }
                    
                } else {
                    return 1
                }
            }
        } else {
            return 0
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameCommunityListCell", for: indexPath) as! HWGameCommunityListCell
        if (models?.count ?? 0) > 0 {
            listCell.model = models?[indexPath.section]
            if (models?[indexPath.section].video.count ?? 0) > 0 {
                if (models?[indexPath.section].image.count ?? 0 ) > 0 {
                    listCell.imageView.kf.setImage(with: URL(string: models?[indexPath.section].image[0] ?? ""))
                } else {
                    
                }
                
            } else {
                if (models?[indexPath.section].image.count ?? 0 ) > 0 {
                    if (models?[indexPath.section].image.count ?? 0 ) == 1 {
                        listCell.imageView.kf.setImage(with: URL(string: models?[indexPath.section].image[0] ?? ""))
                    } else {
                        listCell.imageView.kf.setImage(with: URL(string: models?[indexPath.section].image[indexPath.row] ?? ""))
                    }
                } else {
                }
            }
        } else {
        }
        return listCell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.scrollDidScroll?(scrollView)
    }
}
